﻿using LD.Host;
using System;
using System.Collections.Generic;
using System.Linq;
using YTE.Control;

namespace LD.Tool
{
    public partial class FrmToolBase : FrmOKCancel
    {
        private TB_Tool toolModel = new TB_Tool();
        public TB_Tool TB_Tool
        {
            get
            {
                //toolModel.Name = txt_name.Text;
                return toolModel;
            }
            set
            {
                toolModel = value;
                if (value != null)
                {
                    this.Text = value.Name;
                    this.txt_name.Text = string.IsNullOrEmpty(TB_Tool.Text) ? TB_Tool.Name : TB_Tool.Text;
                }
            }
        }
        public FrmToolBase()
        {
            InitializeComponent();
            this.Shown += FrmToolBase_Shown;
            this.btn_ok.Click += Btn_ok_Click;
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            TB_Tool.Text = this.txt_name.Text;
        }

        private void FrmToolBase_Shown(object sender, EventArgs e)
        {
            if (this.TB_Tool != null)
                this.txt_name.Text = this.TB_Tool.Text;


        }
        public object GetToolValue(TB_Tool model, string flowPath)
        {
            string[] node = flowPath.Split('\\');
            var flow = Appbus.PartNo.Config.Flow.Where(p => p.Name == model.FlowName).FirstOrDefault();
            if (flow != null)
            {
                var tool = flow.Tools.Where(p => p.Name == node[0]).FirstOrDefault();
                if (tool.OutPut == null)
                    return null;
                if (!tool.OutPut.ContainsKey(node[1]))
                    return null;
                return tool.OutPut[node[1]];
            }
            return null;
        }
    }
}
